<!--右下角浮动按钮,滑动时隐藏-->
<template>
	<view>
		<view class="float-btn" @click="onFloatBtnClick" :class="hide ? 'hide' : ''" :style="{bottom:bottom+'rpx'}">
			<text class="float-btn-icon"><text :class="icon"></text></text>
		</view>
	</view>
</template>
<script>
	export default {
		props: {
			url: {
				type: String,
				default: ''
			},
			icon: {
				type: String,
				default: 'cuIcon-add'
			},
      bottom: {
        type: Number,
        default: 210
      }
		},
		data() {
			return {
				hide: false,
				hideTimer: null
			};
		},
		mounted() {
			//  滑动时隐藏
			uni.$on('onPageScroll', () => {
				this.hide = true;
				// 三秒后恢复按钮显示
				if (this.hideTimer) {
					clearTimeout(this.hideTimer)
				}
				this.hideTimer = setTimeout(() => {
					this.hide = false
				}, 500)
			});
		},
		beforeDestroy() {
			uni.$off('onPageScroll');
		},
		methods: {
			onFloatBtnClick() {
				if (this.url) {
					uni.navigateTo({
						url: this.url
					});
				}
				this.$emit('onFloatBtnClick');
			}
		}
	}
</script>
<style lang="scss">
	.float-btn {
		position: fixed;
		right: 15rpx;
		bottom: 200rpx;
		width: 80rpx;
		height: 80rpx;
		border-radius: 10rpx;
		background-color: #ffffff;
		box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.2);
		z-index: 9;
		text-align: center;
		line-height: 80rpx;
		color: #222222;
		//  滑动时隐藏
		transition: all 0.3s;

		&.hide {
			transform: translateX(100%);
		}
	}

	.float-btn-icon {
		font-size: 40rpx;
	}
</style>
